//
//  GGTableViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/3/30.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGTableViewController.h"
#import "GGCarGroupModel.h"
#import "GGCarDetailModel.h"
#import "GGTableViewHeaderView.h"
#import "GGTableViewFooterView.h"
static NSString *cellID = @"cellID";
@interface GGTableViewController ()<UITableViewDelegate,UITableViewDataSource,GGTableViewFooterViewDelegate>
@property (nonatomic, strong) NSMutableArray <GGCarGroupModel *>*dataList;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation GGTableViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"tableView的使用";
    [self loadData];
    [self setUpUI];
}
#pragma mark - setUI
- (void)setUpUI{
    //此处不分组
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    //设置行高
    self.tableView.rowHeight = 70;
    
    //自定义表头表尾
    [self setUpHeaderView];
    [self setUpFooterView];
}
#pragma mark - 设置表头表尾
- (void)setUpHeaderView{
    GGTableViewHeaderView *headerView = [[GGTableViewHeaderView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 350)];
    self.tableView.tableHeaderView = headerView;
}
- (void)setUpFooterView{
    GGTableViewFooterView *footerView = [[GGTableViewFooterView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 70)];
    footerView.delegate = self;
    self.tableView.tableFooterView = footerView;
}
#pragma mark - 表尾的代理方法
- (void)footerselecter{
    NSLog(@"HHH");
}
#pragma mark - 自定义组头组尾
/**
 注意：组头与组尾都有相同的悬停功能
 */
/*
 可以根据不同的需求设置不同组的组头高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    if(section == 0)
//        return 200;
    return GGTableViewHeaderH;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if(section == 0){
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewHeaderH)];
//          view.backgroundColor = [UIColor blueColor];
//          return view;
//    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewHeaderH)];
    view.backgroundColor = [UIColor blueColor];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewHeaderH)];
    title.text = self.dataList[section].title;
    title.textAlignment = NSTextAlignmentCenter;
    [view addSubview:title];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if(section == 0)
//        return 200;
    return GGTableViewFooterH;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if(section == 0){
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewFooterH)];
//        view.backgroundColor = [UIColor redColor];
//        return view;
//    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewFooterH)];
    view.backgroundColor = [UIColor redColor];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), GGTableViewFooterH)];
    title.text = @"footer";
    title.font = [UIFont systemFontOfSize:11];
    [view addSubview:title];
    return view;
}
#pragma mark - tableView的索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self.dataList valueForKey:@"title"];
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList[section].cars.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    GGCarGroupModel *model = self.dataList[indexPath.section];
    GGCarDetailModel *detailModel = model.cars[indexPath.row];
    cell.textLabel.text = detailModel.name;
    cell.imageView.image = [UIImage imageNamed:detailModel.icon];
    return cell;
}
#pragma mark - 组头组尾的文字设置
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return self.dataList[section].title;
//}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"footer";
}
#pragma mark - cell行高
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if(indexPath.row == 0)
//        return 200;
//    return 200;
//}
#pragma mark - data
- (void)loadData{
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"cars_total.plist" withExtension:nil];
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    NSMutableArray *muArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        GGCarGroupModel *model = [GGCarGroupModel groupWithDic:dic];
        [muArray addObject:model];
    }
    self.dataList = muArray.mutableCopy;
    //存在局限性，不能使用model嵌套model的类型
//    self.dataList = [NSArray gg_arrayModelWithPlistName:@"cars_total.plist" withClassName:@"GGCarGroupModel"].mutableCopy;
}
#pragma mark -lazy
- (NSMutableArray <GGCarGroupModel *>*)dataList{
    if(!_dataList){
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
